package com.shushan.discount;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.login.bean.ChildBean;
import com.shushan.service.DialRTAService;
import com.shushan.service.WechatShareRTAService;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.shushanbao.wxapi.WXEntryActivity;
import com.shushan.util.ImageUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.UmengEventUtil;
import com.shushan.util.WxApiUtils;
import com.shushan.util.ZxingUtils;

/**
 * @ClassName: CouponSharePicActivity
 * @Desciption: 优惠券图片分享
 * @author: yichaohua
 * @date: 2018-08-30
 */
public class CouponSharePicActivity extends BaseActivity{

    private static final String TAG = "CouponSharePicActivity";

    private ImageView qrImageView;
    private TextView tvCouponValue;
    private TextView tvTitle;
    private TextView tvUsefulLife;
    private TextView tvParentName;
    private ImageView textImg;
    private LinearLayout layout1;

    private Bitmap qrBitmap;
    private int shareType;//分享类型 1：微信分享 2：朋友圈
    private int couponId;
    private int price;//优惠券价值
    private String name,usefulLife;//优惠券名字，优惠券有效期
    private String shareUrl;//分享二维码网址

    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    onShare();
                    break;
                default:
                    break;
            }
        }
    };

    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.coupon_share_pic_activity);

        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true,this);

        shareType = getIntent().getIntExtra("shareType",1);
        couponId = getIntent().getIntExtra("couponId",0);
        price = getIntent().getIntExtra("price",0);
        name = getIntent().getStringExtra("name");
        usefulLife = getIntent().getStringExtra("usefulLife");
        shareUrl = getIntent().getStringExtra("shareUrl");
        
        initView();

        mHandler.sendEmptyMessageDelayed(0,1000);
    }

    private void initView() {
        qrImageView = findViewById(R.id.coupon_share_qr_img);
        qrBitmap = ZxingUtils.Create2DCode(shareUrl,400,400);
        if(qrBitmap != null){
            qrImageView.setImageBitmap(qrBitmap);
        }
        tvCouponValue = findViewById(R.id.tv_coupon_value);
        tvCouponValue.setText(price+"");
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("价值"+price+"元"+name);
        tvUsefulLife = findViewById(R.id.tv_useful_life);
        tvUsefulLife.setText(usefulLife);
        tvParentName = findViewById(R.id.tv_parent_name);
        tvParentName.setText(getParentName());

        textImg = findViewById(R.id.coupon_share_img);
        layout1 = findViewById(R.id.coupon_share_layout1);
        if(couponId == 4){
            layout1.setVisibility(View.GONE);
            textImg.setVisibility(View.VISIBLE);
        }else {
            layout1.setVisibility(View.VISIBLE);
            textImg.setVisibility(View.GONE);
        }
    }

    private String getParentName(){
        StringBuffer sb = new StringBuffer();
        String childName = null;
        ChildBean currentChildBean = babyApplication.getCurrentChildBean();
        if(currentChildBean != null){
            childName = currentChildBean.childName;
        }
        if(TextUtils.isEmpty(childName)){
            return "";
        }
        int nameLength = childName.length();
        if(nameLength == 1){
            sb.append(childName).append("的家长");
        }else if(nameLength > 1){
            sb.append(childName.charAt(0));
            for(int i=0;i<nameLength-1;i++){
                sb.append("*");
            }
            sb.append("的家长");
        }
        return sb.toString();
    }

    private void onShare() {
        WXEntryActivity.type = 2;

        if(qrBitmap == null){
            showToast("生成二维码失败");
            UmengEventUtil.onEvent(babyApplication,"coupon_share_faild_id","生成二维码失败");
            onBackAction();
        }else {
            boolean result;
            boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);

            if(isChildControl){
                setAllowBack(true);
                Intent intent = new Intent(this,WechatShareRTAService.class);
                intent.putExtra("allowPackName", Constants.ALLOW_WECHAT_PACK_NAME);
                startService(intent);
            }

            if(shareType == 1){
                UmengEventUtil.onEvent(babyApplication,"coupon_wechat_click_id");
                result = WxApiUtils.sharePic(this, WxApiUtils.SHARE_TYPE_SESSION, ImageUtils.screenShotWithoutStatusBar(this));
                Log.d(TAG, "微信分享 = " + result);
            }else {
                UmengEventUtil.onEvent(babyApplication,"coupon_circle_click_id");
                result = WxApiUtils.sharePic(this, WxApiUtils.SHARE_TYPE_TIME_LINE, ImageUtils.screenShotWithoutStatusBar(this));
                Log.d(TAG, "朋友圈分享 = " + result);
            }

            onBackAction();
            if(!result){
                UmengEventUtil.onEvent(babyApplication,"coupon_share_faild_id","分享失败");
                showToast("分享失败");
            }
        }
    }
}
